package me.raih.service.impl;

import me.raih.mapper.BillMapper;
import me.raih.mapper.SalaryMapper;
import me.raih.mapper.SalesMapper;
import me.raih.pojo.po.Bill;
import me.raih.pojo.po.Salary;
import me.raih.pojo.po.Sales;
import me.raih.pojo.vo.Manager;
import me.raih.service.SalaryService;
import me.raih.util.MathUtil;
import me.raih.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author Raih
 * @Time 2021/2/20  13:05
 */
@Service
@Transactional
public class SalaryServiceImpl implements SalaryService {


    @Autowired
    SalaryMapper salaryMapper;

    @Autowired
    SalesMapper salesMapper;

    @Autowired
    BillMapper billMapper;

    @Autowired
    Manager manager;

    @Override
    public List<Salary> findSalaryMessageByUsernameAndTime(String salesName, String time) {
        int i =1 ;
        if(time != null && time.length() > 0){
            try {
                i = Integer.parseInt(time);
            }catch (Exception ignored){
            }
        }
        //定义一个 工资集合
        List<Salary> salaryList = new ArrayList<>();
        //查询出基本信息
        List<Bill> billList = billMapper.findBillByMonth(i, salesName);
        //转换成存放订单信息的map 之所以设置key为 integer类型是因为 key不能相同，不如数字 +1 肯定不同
        Map<Integer,List<Bill>> map = new HashMap<>();
        List<Bill> bills = new ArrayList<>();
        //获取当前日期
        //Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        //设置当前月份
        int nowMonth = calendar.get(Calendar.MONTH)+1;
        //设置当前年份
        int nowYear = calendar.get(Calendar.YEAR);
        //几个月就循环几次
        for (int j = i; j > 0; j--) {
            //遍历订单
            for (Bill bill : billList) {
                ///设置订单月份
                calendar.setTime(bill.getCreateTime());
                int billMonth = calendar.get(Calendar.MONTH) + 1;
                int billYear = calendar.get(Calendar.YEAR);
                int p = nowMonth - billMonth;
                //如果说是跨一年，就 +12
                if (nowYear - billYear == 1) {
                    p += 12;
                }
                if(p == j -1){
                    //将订单放入
                    bills.add(bill);
                }
            }
            //如果不为空就添加到map中，如果为空说明没有该订单信息
            if(bills.size()!=0){
                map.put(j,bills);
                bills = new ArrayList<>();
            }
        }
        //从 map 中获取分月订单信息
        //List<Bill> trueBillList = new ArrayList<>();
        for (Map.Entry<Integer, List<Bill>> integerListEntry : map.entrySet()) {
            //Integer key = integerListEntry.getKey();
            List<Bill> value = integerListEntry.getValue();
            //遍历第一个value，里面存放的都是 某个月全部的数据
            double price = 0.0;
            for (Bill bill : value) {
                price += bill.getAllPrice();
            }
            Salary salary = new Salary();
            //设置 年月 格式
            Date createTime = value.get(0).getCreateTime();
            String s = TimeUtils.changeTimeDesc(createTime);
            salary.setTimeMonth(s);
            //设置本月销售次数
            salary.setMany(value.size());
            //设置业绩
            price = MathUtil.twoDoubleNumber(price);
            salary.setPrice(price);
            //通过username 查找一个 售货员
            Sales sales = salesMapper.findSalesByUsername(salesName);
            //设置售货员名
            salary.setUsername(sales.getName());
            //通过售货员的等级设置基本工资
            double base = manager.getLeaveMap().get(sales.getPower());
            salary.setBase(base);
            //获取奖励金额，即百分比
            double sales_salary = manager.getSalesSalary();
            //计算奖励工资
            double reward = price*sales_salary;
            // 保留两位小数
            reward = MathUtil.twoDoubleNumber(reward);
            //设置奖励工资
            salary.setReward(reward);
            //这次暂时忽略请假
            salary.setTrueReward(reward+base);
            salaryList.add(salary);
        }
        return salaryList;
    }

    @Override
    public int updateSalesBySales(Sales sales) {

        int i = salesMapper.updateSales(sales);

        return i;
    }
}
